//
//  LGSliderDrawerManager.m
//  LGSliderDrawer
//
//  Created by apple on 2021/3/24.
//

#import "LGSliderDrawerManager.h"

#import "LGSliderDrawerContentView.h"

@interface LGSliderDrawerManager ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIView *superView;

@property (nonatomic, strong) LGSliderDrawerContentView *contentView;

@property (nonatomic, strong) UIView *rightView;

@property (nonatomic, strong) LGSliderDrawerConfigure *configuration;

@end


@implementation LGSliderDrawerManager

/**
 呼出侧滑控制器的方法（主要）

 @param rightView 需要展示抽屉效果的子视图
 @param superView 父视图
 @param gestureView 添加手势的视图
 @param configuration 侧滑过程的一些参数配置，如果传nil会创建一个默认的配置参数
 */
- (void)showDrawerRightView:(UIView *)rightView
                  superView:(UIView *)superView
                gestureView:(UIView *)gestureView
              configuration:(LGSliderDrawerConfigure *)configuration {
    self.rightView = rightView;
    self.superView = superView;
    self.configuration = configuration;
    
    [self.contentView removeFromSuperview];
    self.contentView.baseContainer = rightView;
    self.contentView.hidden = YES;
    [superView addSubview:self.contentView];

    if (gestureView) {
        if (configuration.gestureType == LGSliderDrawerGestureTypePan) {
            ///侧滑拖动手势
            UIPanGestureRecognizer *edgePanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
            edgePanGesture.delegate = self;
            [gestureView addGestureRecognizer:edgePanGesture];
            
            if ([gestureView isKindOfClass:[UIScrollView class]]) {
                UIScrollView *scrollView = (UIScrollView *)gestureView;
                /// 自定义滑动手势优先级高于baseScroll滑动手势
                [scrollView.panGestureRecognizer requireGestureRecognizerToFail:edgePanGesture];
            }
        }else {///边缘侧滑拖动手势
            UIScreenEdgePanGestureRecognizer *edgePanGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
            edgePanGesture.edges = UIRectEdgeRight;
            edgePanGesture.delegate = self;
            [gestureView addGestureRecognizer:edgePanGesture];
            
            if ([gestureView isKindOfClass:[UIScrollView class]]) {
                UIScrollView *scrollView = (UIScrollView *)gestureView;
                /// 自定义滑动手势优先级高于baseScroll滑动手势
                [scrollView.panGestureRecognizer requireGestureRecognizerToFail:edgePanGesture];
            }
        }
    }
}

//MARK: - 拖动手势处理方法
- (void)panAction:(UIPanGestureRecognizer *)panGesture {

    // 获取平移手势移动后, 在相对坐标中的偏移量
    CGPoint curPoint = [panGesture translationInView:self.superView];
    CGFloat curPointX = curPoint.x;
    if (curPointX<0) {
        curPointX = kScreenWidth + curPointX;
    }else {
        curPointX = kScreenWidth - curPointX;
    }

    if (panGesture.state == UIGestureRecognizerStateBegan) {// 手势点击
        if (self.showRightViewBlock) {
            /// 手势开始点击，是否需要更新子视图
            UIView *view = self.showRightViewBlock();
            if (view) {
                [self updateRightView:view];
            }
        }
        
        if (!self.contentView.superview) {
            [self.superView addSubview:self.contentView];
            self.contentView.baseContainer = self.rightView;
        }
        /// 更新侧滑状态
        [self.contentView updateSidelip:YES];
    }else if (panGesture.state == UIGestureRecognizerStateEnded) {// 手势结束
        /// 更新侧滑状态
        [self.contentView updateSidelip:NO];
        CGFloat width = self.configuration.distance * kScreenWidth;
        CGFloat baseContainer_originX = kScreenWidth - width;
        if (curPointX > kScreenWidth - width/2.0) {
            [self.contentView dismiss];
        }else if (curPointX != baseContainer_originX) {
            [self.contentView show];
        }
    }else if (panGesture.state == UIGestureRecognizerStateChanged) {//手势拖拽
        self.contentView.configuration = self.configuration;
        [self.contentView showWithPointX:curPointX];
    }
}

//MARK: - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (self.js_gestureRecognizerShouldBegin) {
        return self.js_gestureRecognizerShouldBegin(gestureRecognizer);
    }
    return YES;
}

/// 判断手势方向是否是向左滑动
- (BOOL)isLeftDirectionTranslation:(CGPoint)translation {
    /// 默认右滑
    UISwipeGestureRecognizerDirection direction = UISwipeGestureRecognizerDirectionRight;
    CGFloat absX = fabs(translation.x);
    CGFloat absY = fabs(translation.y);
    if (absX > absY ) {
        if (translation.x<0) {
            //向左滑动
            direction = UISwipeGestureRecognizerDirectionLeft;
        }else{//向右滑动
            direction = UISwipeGestureRecognizerDirectionRight;
        }
    } else if (absY > absX) {
        if (translation.y<0) {//向上滑动
            direction = UISwipeGestureRecognizerDirectionUp;
        }else{//向下滑动
            direction = UISwipeGestureRecognizerDirectionDown;
        }
    }
    return direction == UISwipeGestureRecognizerDirectionLeft;
}

//MARK: - 展示动画
/// 更新侧滑状态
- (void)updateSidelip:(BOOL)isSidelip {
    [self.contentView updateSidelip:isSidelip];
}

- (void)show {
    if (!self.contentView.superview) {
        [self.superView addSubview:self.contentView];
        self.contentView.baseContainer = self.rightView;
    }
    [self.contentView show];
}

- (void)dismiss {
    [self.contentView updateSidelip:NO];
    [self.contentView dismiss];
}

- (LGSliderDrawerContentView *)contentView {
    if (!_contentView) {
        _contentView = [[LGSliderDrawerContentView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    }
    return _contentView;
}

- (void)updateRightView:(UIView *)rightView {
    [self.rightView removeFromSuperview];
    self.rightView = rightView;
    self.contentView.baseContainer = self.rightView;
}

- (void)dealloc {
    NSLog(@"JSSliderDrawerManager dealloc");
}


@end
